//
//  CYShakeIMG.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/10/7.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//
#define _kAUTO(x) (AUTO(x)*AUTO(1)*AUTO(1))
#import "CYShakeIMG.h"
@interface CYShakeIMG()
@property (nonatomic, strong) CAAnimationGroup *groupAnimation;
@property (nonatomic, strong) CABasicAnimation *scaleAnimation;
@property (nonatomic, strong) CABasicAnimation *rotateAnimation;
@end
@implementation CYShakeIMG
- (instancetype)initWithImageName:(NSString *)imageName{
    self = [super init];
    if (self) {
        self.image = [UIImage imageNamed:imageName];
        CGSize size = CGSizeMake(AUTO(240), self.image.size.height * 1.0 / self.image.size.width * AUTO(240));
        self.bounds = CGRectMake(0.0, 0.0, size.width, size.height);
        [self setupShapeLayer];
        [self setupBall];
    }
    return self;
}

- (NSMutableArray *)array{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (CAAnimationGroup *)groupAnimation{
    if (!_groupAnimation) {
        _groupAnimation = [CAAnimationGroup animation];
        _groupAnimation.duration = 2.0f;
        _groupAnimation.speed = 2;
    }
    return _groupAnimation;
}

- (CABasicAnimation *)scaleAnimation{
    if (!_scaleAnimation) {
        _scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        _scaleAnimation.fromValue = @(0.5);
        _scaleAnimation.toValue = @(1.0);
        _scaleAnimation.toValue = @(0.5);
        _scaleAnimation.duration = 1.0f;
        _scaleAnimation.repeatCount = MAXFLOAT;
        _scaleAnimation.speed = 5;
    }
    return _scaleAnimation;
}

- (CABasicAnimation *)rotateAnimation{
    if (!_rotateAnimation) {
        _rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        _rotateAnimation.fromValue = @(0.0);
        _rotateAnimation.toValue = @(6.0 * M_PI);
        _rotateAnimation.duration = 1.0f;
        _rotateAnimation.repeatCount = MAXFLOAT;
        _rotateAnimation.speed = 5;
    }
    return _rotateAnimation;
}

- (void)setupShapeLayer{
    CGFloat offsetX = (self.bounds.size.width - AUTO(20)) / 5.0;//去除图片边框的宽度，
    for (int i = 1; i < 5; i++ ) {
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.lineWidth = 1;
        shapeLayer.strokeColor = RGBA(254, 210, 89, 1).CGColor;
        shapeLayer.lineCap = kCALineCapRound;
        shapeLayer.bounds = self.bounds;
        shapeLayer.position = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0);
        
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, offsetX * i + AUTO(10), self.bounds.size.height / 2.0 - 5);
        CGPathAddLineToPoint(path, NULL, offsetX * i + AUTO(10), self.bounds.size.height * 4.0 / 5 - 2);
        shapeLayer.path = path;
        CGPathRelease(path);
        [self.layer addSublayer:shapeLayer];
    }
}

- (void)setupBall{
    CGFloat offsetX = AUTO(10);//距离图片边框的宽度
    CGFloat labelWidth = (self.bounds.size.width - AUTO(20)) / 5.0 - offsetX * 2;
    for (int i = 0; i < 5; i++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = [NSString stringWithFormat:@"%@",@(arc4random()%10)];
        [self.array addObject:label.text];
        label.bounds = CGRectMake(0.0, 0.0, labelWidth, labelWidth);
        label.layer.cornerRadius = label.bounds.size.width / 2.0;
        label.layer.masksToBounds = YES;
        label.backgroundColor = THEME_COLOR;
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = 200 + i;
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(AUTO(10) + offsetX + (offsetX * 2+ labelWidth) * i);
            make.top.equalTo(self).with.offset(self.bounds.size.height / 2.0 - AUTO(3) + offsetX);
            make.size.mas_equalTo(label.bounds.size);
        }];
    }
}

- (void)shakeMethod{
    [self.array removeAllObjects];
    
    for (int i = 0; i < 5; i ++) {
        UILabel *label = [self viewWithTag:200+i];
        
        label.text = [NSString stringWithFormat:@"%@",@(arc4random()%10)];
        [self.array addObject:label.text];
        self.groupAnimation.beginTime = CACurrentMediaTime() + i * 0.1;
        _groupAnimation.animations = @[[self moveAnimation:label.frame], self.scaleAnimation, self.rotateAnimation];
        [label.layer addAnimation:self.groupAnimation forKey:@"groupAnnimation"];
    }
}

- (CABasicAnimation *)moveAnimation:(CGRect)rect{
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(rect.origin.x + 20, rect.origin.y)];
    moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(rect.origin.x + 20, CGRectGetMaxY(rect))];
    moveAnimation.duration = 1.0f;
    moveAnimation.repeatCount = MAXFLOAT;
    moveAnimation.speed = 5;
    return moveAnimation;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
